package cn.xuetian.springboot;


import cn.xuetian.springboot.data.App;
import cn.xuetian.springboot.mapper.IAppMapper;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import org.springframework.util.Assert;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@ImportResource(locations = {"bean-config.xml"})
@SpringBootApplication
public class Starter {

    /**
     * https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {

        SpringApplication.run(Starter.class, args);

        aaa();


        System.out.println("server start up done...");
    }

    public static void aaa() throws IOException {

        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        try (SqlSession session = sqlSessionFactory.openSession()) {

            IAppMapper mapper = session.getMapper(IAppMapper.class);
            List<App> list = mapper.getList();


            //分页插件支持以下几种调用方式：
            //第一种，RowBounds方式的调用
            List<App> a = session.selectList("getList", null, new RowBounds(0, 10));


            //第二种，Mapper接口方式的调用，推荐这种使用方式。
            PageHelper.startPage(1, 10);
            List<App> b = mapper.getList();

            //第三种，Mapper接口方式的调用，推荐这种使用方式。
            PageHelper.offsetPage(1, 10);
            List<App> c = mapper.getList();

            Assert.isTrue(new Date() != null);
        } catch (Exception e) {
            e.printStackTrace();
        }

//        DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
//        TransactionFactory transactionFactory =
//                new JdbcTransactionFactory();
//        Environment environment =
//                new Environment("development", transactionFactory, dataSource);
//        Configuration configuration = new Configuration(environment);
//        configuration.addMapper(BlogMapper.class);
//        SqlSessionFactory sqlSessionFactory =
//                new SqlSessionFactoryBuilder().build(configuration);
    }
}
